package com.lagou.edu.controller;

import com.lagou.edu.session.SessionStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class LoginController {

    private static final String DEFAULT_USER = "admin";
    private static final String DEFAULT_PASSWORD = "admin";

    @Autowired
    private SessionStorage sessionStorage;

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public void login(HttpServletRequest request, HttpServletResponse response, String username, String password)
        throws IOException {
        if (DEFAULT_USER.equals(username) && DEFAULT_PASSWORD.equals(password)) {
            sessionStorage.storeSession(username);
            request.getSession().setAttribute("username", username);
            response.sendRedirect("/resume/list");
        } else {
            response.sendRedirect("/login.jsp");
        }
    }
}
